package 笔试.字节笔试.有序链表每个元素出现两次;

import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);

        while (sc.hasNext())
        {
            String[] split = sc.nextLine().split(" ");
            int[] res=new int[split.length];
            for (int i = 0; i < res.length; i++)
            {
                res[i]=Integer.valueOf(split[i]);
            }

            ListNode pre=new ListNode(res[0]);
            ListNode head=pre;
            ListNode node=null;
            int cnt=1;

            for (int i = 1; i < res.length; i++)
            {
                node=new ListNode(res[i]);
                pre.next=node;
                pre=node;
                cnt++;
            }

            pre.next=null;
            int [] list=new int[cnt*2];
            Main m=new Main();
            m.method(head,list);
            for(int i=0; i<list.length;i++)
            {
                if(list[i]!=0)
                    System.out.print(list[i]+" ");
            }
        }

    }
    public int[] method(ListNode head,int[] list)
    {
        //定义指针
        int p=0;

        int lastval=head.val;
        int count=1;
        ListNode node=head.next;
        list[p]=head.val;
        p++;
        while(node!=null)
        {
            if(node.val!=lastval)
            {
                if(count==1)//前一个数字次数为1的情况
                {
                    list[p++]=lastval;
                    list[p]=node.val;
                    lastval=node.val;
                    count=1;
                }
                else
                {
                    list[p++]=node.val;
                    lastval=node.val;//更新最近一次的值
                    count=1;
                }
            }
            else//当前结点与上一个结点值相等
            {
                count++;
                if(count==2)//出现第二次
                {
                    list[p++]=node.val;
                    lastval=node.val;
                }
                //出现次数超i过两次
            }
            node=node.next;
            p++;
        }
        if (count==1)
            list[p]=lastval;
        return list;
    }
}
class ListNode
{
    int val;
    ListNode next=null;
    public ListNode(int val)
    {
        this.val=val;
    }
}

